from api import models
import  json
#根据用户名查询用户的动态
from django.db.models import Count
def dynamics(username,request):
    data = {
        'code': 200,
        'msg': 'error',
        'result': '没有这个查询模块'
    }
    if username != None:

        obj = models.Dynamic.objects.filter(launch_user=username)
        if not obj:
            data['result'] = '没有此账号'
            data['code'] = 1002
            return data
        dynamics_list = []
        if obj :
            for dynamic in obj:
                dynamics_list.append(dynamic.context)
            data['msg'] = 'ok'
            data['result'] = dynamics_list
            return data
    else:
        data['result'] = '用户不能为空'
        return data

#查询基本个人信息
#昵称 电子邮箱 年龄 认证情况 真实姓名 公益积分 个人介绍 头像
#根据传入的参数
def self_data(username,request):
    data = {
        'code': 1001,
        'msg': 'error',
        'result': '没有传入用户id'
    }

    if username == None:
        return data
    obj = models.User.objects.filter(username=username).values('username','nickname','email',
                                                               'certification','identity',
                                                               'integral','introduction',
                                                               'avatar').first()
    print(obj)
    # 如果没有此用户
    if not obj:
        data['msg'] = 'error'
        data['result'] = '没有这个用户'
        return data
    user = {'username':obj['username']}
    informations = ['username','nickname','email','certification','identity','integral','introduction','avatar']
    for i in informations:
        temp = request.POST.get(i)
        if temp == '1':
            user[i] = obj[i]

    data['result'] = user
    return data
#查询 我关注的 关注我的
def attention(username,request):
    data = {
        'code': 1001,
        'msg': 'error',
        'result': '没有传入用户id'
    }
    if username == None:
        return data

    #我关注的用户
    obj1 = models.Attention.objects.filter(username=username).values('attention_user')
    #关注我的用户
    obj2 = models.Attention.objects.filter(attention_user=username).values('username')
    data['result'] = {
        "i":[temp for temp in obj1],
        'o':[temp for temp in obj2]
    }
    data['code'] = 200
    data['msg'] = 'ok'
    return data
def attention_dynamics(username,request):
    data = {
        'code': 200,
        'msg': 'ok',
        'result': '没有传入用户id'
    }
    if username == None:
        return data
    # 我关注的用户
    obj1 = models.Attention.objects.filter(username=username).values_list("attention_user")

    page = request.POST.get('page')
    context_list = []
    if page == None:
        page = 0
        content = models.Dynamic.objects.filter(launch_user__in=[obj for obj in obj1]).all()[page:10]
        print(content)
        for con_obj in content:

            temp = {
                "launch_user": con_obj.launch_user.username,
                "add_date": con_obj.add_date,
                "like": con_obj.like,
                'context': con_obj.context
            }
            print(temp)
            context_list.append(temp)
        data['result'] = context_list
    else:
        if page.isdigit():
            page = int(page)
        else:
            page = 0
        content = models.Dynamic.objects.filter(launch_user__in=[obj for obj in obj1])[page*10:page*10+10]
        print(content)
        for con_obj in content:
            temp = {
                "launch_user":con_obj.launch_user.username,
                "add_date":con_obj.add_date,
                "like":con_obj.like,
                'context':con_obj.context
            }
            context_list.append(temp)
        data['result'] = context_list

    return data
#收藏 查询用户收藏的文章
def favorite(username,request):
    data = {
        'code': 200,
        'msg': 'error',
        'result': '没有这个查询模块'
    }
    if username != None:
        projects_ids = models.Favorite.objects.filter(username=username).values('id',"pid__title",'pid__add_date','username__avatar','username__nickname')
        projects = []
        for id in projects_ids:
            temp_id = id["id"]
            count = len(models.Favorite.objects.filter(pid=temp_id))
            comment = len(models.Comments_projects.objects.filter(project_id=temp_id))
            print(id,count,comment)
            project = dict(id)
            project['count'] = count
            project['comment'] = comment
            projects.append(project)
        data['code'] = 200
        data['msg'] = 'ok'
        data['result'] = projects
        return data
    else:
        return data

#查询活动（未结束）
def query_project(username,request):
    obj = [i for i in models.Projects.objects.filter(finish=0).values('id',"community_id",'title','num','details','purpose','progress','add_date')]
    data = {
        'code': 200,
        'msg': 'ok',
        'result': obj
    }
    return data

#查询用户加入的社团的id
def communitied(username,request):
    data = {
        'code': 200,
        'msg': 'ok',
        'result': []
    }
    if username != None:
        obj = models.Join_community.objects.filter(username=username).values('community_id','community_id__avatar','community_id__name','community_id__num')
        data['result'] = list(obj)
        return data
    else:
        return data

#排行榜相关
def leaderboard(username,request):
    data = {
        'code': 200,
        'msg': 'ok',
        'result': []
    }
    leaderboard = request.POST.get('leaderboard')

    if leaderboard == 0:
        #活跃榜
        pass
    elif leaderboard == '1':
        #公益积分榜
        objs = list(models.User.objects.all().order_by("-integral"))[0:10]
        bordlist = []
        for obj in objs:
            temp = {
                'img': obj.avatar,
                'title': obj.nickname,
                'count': obj.integral
            }
            bordlist.append(temp)
        data['result'] = bordlist
        return data
    elif leaderboard == '2':
        #优秀社团榜
        objs = list(models.Community.objects.all().order_by("-project_num"))[0:10]
        bordlist = []
        for obj in objs:
            temp = {
                'img':obj.avatar,
                'title':obj.name,
                'count':obj.project_num
            }
            bordlist.append(temp)
        data['result'] = bordlist
        return data
    else:
        return data

def join_project(username,request):
    data = {
        'code': 110,
        'msg': 'error',
        'result': '作废'
    }
    project_id = request.POST.get("ProjectId")
    if project_id != None:
        obj = models.Join_project.objects.filter(project_id=project_id).count()
    return data

